<?php

namespace App\Models\OpenShop\InvestmentPromotion;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Support\ChangeSupport;

class OpenShopSignOrderPayHistoryModel extends BaseModel
{
    use KfSoftDeletes;
    protected $table = 'open_shop_sign_order_pay_history';

    public static array $orderLogTranslateConfig = [
        "proofImg" => [
            "type" => ChangeSupport::FILED_TYPE_PRIVATE_PICTURE,
            "desc" => "支付凭证图片",
        ],
        "price"    => [
            "type" => ChangeSupport::FILED_TYPE_PRICE,
            "desc" => "支付金额",
        ]
    ];

    const PAY_STATUS_OK   = 1; // 已支付
    const PAY_STATUS_WAIT = 0; // 待支付

    const STATUS_MAP = [
        self::PAY_STATUS_WAIT => "待支付",
        self::PAY_STATUS_OK   => "已支付"
    ];

    const TYPE_DEPOSIT = 1; // 定金
    const TYPE_FINAL   = 2; // 尾款

    // PAY_CHANNEL枚举详见CheckEnum
    const PAY_CHANNEL_ALI     = 1; // 支付宝
    const PAY_CHANNEL_WX      = 2; // 微信
    const PAY_CHANNEL_OFFLINE = 5; // 线下支付（暂时只有招商银行公账）

    const PAY_CHANNELS = [
        self::PAY_CHANNEL_ALI     => '支付宝',
        self::PAY_CHANNEL_WX      => '微信',
        self::PAY_CHANNEL_OFFLINE => '线下支付',
    ];


    protected $fillable = [
        'orderNo',
        'shouldPayPrice',
        'payPrice',
        'payChannel',
        'type',
        'payStatus',
        'payTime',
        'subject',
        'sysType',
        'bizId',
        'payNo',
        'createdByUid',
        'updatedByUid',
    ];

    public function signOrderRel()
    {
        return $this->hasOne(OpenShopSignOrderModel::class, 'orderNo', 'orderNo');
    }

    public function mainProcessRel()
    {
        return $this->hasOne(OpenShopMainProcessModel::class, 'orderNo', 'orderNo');
    }

    /**
     * 新增支付流水
     * @param string $orderNo
     * @param int $shouldPayPrice
     * @param int $payPrice
     * @param int $payChannel
     * @param int $type
     * @param string $subject
     */
    public static function addHistory(
        string $orderNo,
        int $shouldPayPrice,
        int $payPrice,
        int $payChannel,
        int $type,
        string $subject,
        int $sysType,
        string $bizId,
        int $payStatus = self::PAY_STATUS_WAIT,
        int $payTime = 0,
        string $payNo = '',
        int $currentUid = 0
    ) {
        $data = [
            'orderNo'        => $orderNo,
            'shouldPayPrice' => $shouldPayPrice,
            'payPrice'       => $payPrice,
            'payChannel'     => $payChannel,
            'type'           => $type,
            'payStatus'      => $payStatus,
            'payTime'        => $payTime,
            'subject'        => $subject,
            'sysType'        => $sysType,
            'bizId'          => $bizId,
            'payNo'          => $payNo,
        ];

        if ($currentUid) {
            $data['createdByUid'] = $data['updatedByUid'] = $currentUid;
        }

        $obj = self::query()->create($data);

        return $obj->id;
    }

    /**
     * 支付成功
     * @param int $id
     * @param int $payTime
     * @return int
     */
    public static function paySuccess(int $id, int $payTime = 0)
    {
        return self::query()
                   ->where('id', $id)
                   ->update(['payStatus' => self::PAY_STATUS_OK, 'payTime' => $payTime ?: time()]);
    }

    /**
     * 更新payNo
     * @param int $id
     * @param string $payNo
     * @return mixed
     */
    public static function updatePayNo(int $id, string $payNo)
    {
        return self::query()->where('id', $id)->update(['payNo' => $payNo]);
    }

    public static function deleteData(int $id): int
    {
        return self::query()->where('id', $id)->update(['isDeleted' => 1]);
    }
}
